package Delegate;


import gui.BuddyList;
import protocol.generic.AbstractProtocolManager;

import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.core.QRect;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QBrush;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QFont;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QItemDelegate;
import com.trolltech.qt.gui.QLinearGradient;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QStyle;
import com.trolltech.qt.gui.QStyleOptionViewItem;
import com.trolltech.qt.core.Qt.AlignmentFlag;

public class GroupEntryDelegate extends QItemDelegate {
	
	private BuddyList buddyList;
	
	
	public void setBuddyList(BuddyList buddyList){
		this.buddyList=buddyList;
	}
	
	protected void drawDisplay(QPainter painter, QStyleOptionViewItem option, QRect rect, String text) {
		rect.setHeight(16);
		String ss[]=text.split("#");
		if(ss.length==1){
			rect.setHeight(32);
		}
		painter.drawText(rect, AlignmentFlag.AlignVCenter.value(), ss[0]);
		rect.moveTop(rect.top() + 16);

		painter.setPen(QColor.gray);
		if(ss.length!=1)
			painter.drawText(rect, 0, ss[1]);

	}
	
	/*
	protected void drawDecoration(QPainter painter, QStyleOptionViewItem option, QRect rect, QPixmap pixmap) {
		
	}
	*/
	
	
	public void paint(QPainter painter, QStyleOptionViewItem option, QModelIndex index) {
		Object data = index.data();
		if (data != null){
			QLinearGradient gradient=new QLinearGradient(option.rect().left(),option.rect().top(),option.rect().left(),option.rect().bottom());
			QRect rectTemp = option.rect();
			String text=(String)(index.model().data(index, Qt.ItemDataRole.DisplayRole));
			String ss[]=text.split("#");
			if(ss.length!=1)
				rectTemp.setLeft(-20);
			if(option.state().isSet(QStyle.StateFlag.State_Selected)){
        		gradient.setColorAt(0, new QColor(110,161,241));
            	gradient.setColorAt(1, new QColor(86,125,188));
                painter.fillRect(rectTemp, new QBrush(gradient));
//				painter.fillRect(option.rect(), option.palette().highlight());
        	}
			else if (option.state().isSet(QStyle.StateFlag.State_MouseOver)) {
        		gradient.setColorAt(0, new QColor(231,239,253));
            	gradient.setColorAt(1, new QColor(203,218,241));
                painter.fillRect(rectTemp, new QBrush(gradient));
            }
		}
		
		
		QIcon icon = (QIcon)(index.model().data(index, Qt.ItemDataRole.DecorationRole));
		QRect rect = option.rect();
		int width= rect.right();
		rect.setRight(rect.left() + 32);
		option.setRect(rect);
		
		this.drawDecoration(painter, option, option.rect(), icon.pixmap(22, 22));
		rect.setRight(width-64);
		rect.setLeft(rect.left() + 32);
		option.setRect(rect);
		String text=(String)(index.model().data(index, Qt.ItemDataRole.DisplayRole));
		String ss[]=text.split("#");
		this.drawDisplay(painter, option, option.rect(), text);
		
		//draw the protocol icon
		rect.setLeft(width-32);
		rect.setRight(width);
		if(ss.length!=1){
			if(ss[1].equalsIgnoreCase("unavailable")){
				if(ss[2].equalsIgnoreCase("Gtalk"))
					this.drawDecoration(painter, option, rect, new QIcon(new QPixmap("classpath:resources/google-talk.png")).pixmap(22, 22, QIcon.Mode.Disabled, QIcon.State.On));
				else if(ss[2].equalsIgnoreCase("Jabber"))
					this.drawDecoration(painter, option, rect, new QIcon(new QPixmap("classpath:resources/jabber.png")).pixmap(22, 22, QIcon.Mode.Disabled, QIcon.State.On));
				else if(ss[2].equalsIgnoreCase("Yaim"))
					this.drawDecoration(painter, option, rect, new QIcon(new QPixmap("classpath:resources/aim.png")).pixmap(22, 22, QIcon.Mode.Disabled, QIcon.State.On));
			}
			else{
				if(ss[2].equalsIgnoreCase("Gtalk"))
					this.drawDecoration(painter, option, rect, new QIcon(new QPixmap("classpath:resources/google-talk.png")).pixmap(22, 22));
				else if(ss[2].equalsIgnoreCase("Jabber"))
					this.drawDecoration(painter, option, rect, new QIcon(new QPixmap("classpath:resources/jabber.png")).pixmap(22, 22));
				else if(ss[2].equalsIgnoreCase("Yaim"))
					this.drawDecoration(painter, option, rect, new QIcon(new QPixmap("classpath:resources/aim.png")).pixmap(22, 22));
			}
			
			rect.setLeft(width-76);
			rect.setRight(width-54);
			if(ss[4].equalsIgnoreCase("true"))
				this.drawDecoration(painter, option, rect, new QIcon(new QPixmap("classpath:resources/blocked.png")).pixmap(22, 22));
		}

		
		//draw the avatar icon, if the groupentry hasn't, draw the default icon
		rect.setLeft(width-54);
		rect.setRight(width-32);
		if(ss.length!=1)
			this.drawDecoration(painter, option, rect, buddyList.getPixmapByEntry(buddyList.getGroupEntryByID(ss[3])));

	}

}
